<template>
    <div class="edu-page-wrap apply-template">
        <!-- 筛选条件 -->
        <condition-card @reset="reset" @search="filterCondition">
            <el-row>
                <el-col :span="8">
                    <el-form-item label="年级">
                        <el-date-picker
                            style="width: 100%;"
                            v-model="query.currentGrade"
                            type="year"
                            value-format="yyyy"
                            placeholder="请选择"
                            @change="
                                currentDptId === '1'
                                    ? handleChangeFaculty()
                                    : ''
                            "
                            :picker-options="pickerOptions"
                        ></el-date-picker>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="学院">
                        <DictSelect
                            labelAll="请选择"
                            v-model="query.currentFaculty"
                            :data="options.faculty"
                            @change="handleChangeFaculty"
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="培养专业">
                        <DictSelect
                            labelAll="请选择"
                            v-model="query.currentProfession"
                            :data="options.profession"
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="培养层次">
                        <DictSelect
                            labelAll="请选择"
                            v-model="query.trainingLevel"
                            :data="options.trainingLevel"
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="培养类别">
                        <DictSelect
                            labelAll="请选择"
                            v-model="query.trainingCategory"
                            :data="options.trainingCategory"
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="学位类型">
                        <DictSelect
                            labelAll="请选择"
                            v-model="query.degreeType"
                            :data="options.degreeType"
                            @change="
                                currentDptId !== '1'
                                    ? handleChangeFaculty()
                                    : ''
                            "
                        ></DictSelect>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label="学习形式">
                        <DictSelect
                            labelAll="请选择"
                            v-model="query.formLearning"
                            :data="options.formLearning"
                        ></DictSelect>
                    </el-form-item>
                </el-col>
            </el-row>
        </condition-card>
        <el-card shadow="never">
            <h2>{{this.applyType == 'batchApply'?"批量申请异动列表":"教务员代申请列表"}}</h2>
            <div class="card-title">
                <KeywordInput
                    v-model="query.keyWord"
                    @enter="filterCondition"
                    placeholder="请输入学生学号/姓名搜索"
                ></KeywordInput>
                <el-button
                    type="primary"
                    @click="jumpPage"
                >{{ applyType === "batchApply" ? "批量异动" : "代理申请" }}</el-button>
            </div>
            <edu-table
                :order="false"
                :data="applyList.data"
                :total="applyList.total"
                @page-change="pageChange"
                @selection-change="handleSelectionChange"
            >
                <el-table-column label="学号" show-overflow-tooltip>
                    <template slot-scope="scope">{{ scope.row.studentId }}</template>
                </el-table-column>
                <el-table-column label="姓名" show-overflow-tooltip>
                    <template slot-scope="scope">{{ scope.row.studentName }}</template>
                </el-table-column>
                <el-table-column label="年级" show-overflow-tooltip>
                    <template slot-scope="scope">{{ scope.row.currentGrade }}</template>
                </el-table-column>
                <el-table-column label="学院" show-overflow-tooltip>
                    <template slot-scope="scope">{{ scope.row.currentFacultyI18n }}</template>
                </el-table-column>
                <el-table-column label="培养专业" show-overflow-tooltip v-if="global.projectId == 1">
                    <template slot-scope="scope" >
                        {{ scope.row.currentProfessionName }}
                    </template>
                </el-table-column>
                <el-table-column label="培养专业" show-overflow-tooltip  v-else-if="global.projectId == 2">
                    <template slot-scope="scope" >
                        {{ scope.row.currentProfessionI18n }}
                    </template>
                </el-table-column>
                <el-table-column label="培养层次" show-overflow-tooltip>
                    <template slot-scope="scope">{{ scope.row.trainingLevelI18n }}</template>
                </el-table-column>
                <el-table-column label="培养类别" show-overflow-tooltip>
                    <template slot-scope="scope">{{ scope.row.trainingCategoryI18n }}</template>
                </el-table-column>
                <el-table-column label="学位类型" show-overflow-tooltip>
                    <template slot-scope="scope">{{ scope.row.degreeTypeI18n }}</template>
                </el-table-column>
                <el-table-column label="学习形式" show-overflow-tooltip>
                    <template slot-scope="scope">{{ scope.row.formLearningI18n }}</template>
                </el-table-column>
                <el-table-column label="导师" width="120">
                    <template slot-scope="scope">{{ scope.row.teacherName }}</template>
                </el-table-column>
                <el-table-column label="操作" v-if="applyType !== 'batchApply'" width="90">
                    <template slot-scope="scope">
                        <el-button type="text" @click="jumpPage(scope.row, 0)">代理申请</el-button>
                        <!-- <template v-else>
                            <el-button
                                type="text"
                                @click="jumpPage(scope.row, 1)"
                                >查看</el-button
                            >
                            <el-button
                                type="text"
                                @click="jumpPage(scope.row, 0)"
                                >编辑</el-button
                            >
                        </template>-->
                    </template>
                </el-table-column>
            </edu-table>
        </el-card>
    </div>
</template>
<script>
import { queryDic } from "common/src/api/dictionary";
import UTILS from "common/src/utils/utils";
import * as API from "common/src/api/studentStatus/applicationByAcademic";
import mixin from 'common/src/mixins/index';
export default {
    name: "applyTemplate",
    mixins: [mixin],
    props: {
        applyType: {
            type: String,
            default: ""
        }
    },
    data() {
        return {
            query: {
                currentGrade: "",
                keyWord: "",
                trainingLevel: "",
                currentFaculty: "",
                currentProfession: "",
                trainingCategory: "",
                degreeType: "",
                formLearning: "",
                pageNum_: 1,
                pageSize_: 20,
                isStudent: ""
            },
            options: {
                trainingLevel: [],
                faculty: [],
                profession: [],
                formLearning: [],
                degreeType: [],
                trainingCategory: []
            },
            selectedStu: [], //多选保存的学生信息列表
            applyList: {
                total: 0,
                data: []
            },
            currentDptId: "",
            pickerOptions: {
                disabledDate(time) {
                    return (
                        time.getTime() > Date.now() ||
                        time.getTime() < new Date("1978").getTime()
                    );
                }
            },
        };
    },
    created() {
        this.query.isStudent = this.global.isStudent;
        if (this.mockUser) {
            this.currentDptId = this.mockUser.currentManageDptId;
        } else if (this.currentUser) {
            this.currentDptId = this.currentUser.currentManageDptId;
        }
        this.initOptions();
        this.getApplyList();
    },
    methods: {
        // 获取筛选条件字段下拉数据
        initOptions() {
            const data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: ["X_YX", "X_PYCC", "X_PYLB", "X_XWLX", "X_XXXS"]
            };
            queryDic(data).then(res => {
                if (res.code === 200) {
                    UTILS.fillSelect(
                        {
                            faculty: "X_YX",
                            trainingLevel: "X_PYCC",
                            trainingCategory: "X_PYLB",
                            degreeType: "X_XWLX",
                            formLearning: "X_XXXS"
                        },
                        res.data,
                        this.options
                    );
                }
            });
        },
        // 根据学院获取专业
        handleChangeFaculty(val) {
            this.options.profession = [];
            this.query.currentProfession = "";
            let facultys, degreeType;
            facultys = this.query.currentFaculty
                ? [this.query.currentFaculty]
                : [];
            degreeType = this.query.degreeType ? [this.query.degreeType] : [];
            const data = {
                lang: this.$store.getters.language || "cn",
                type: "allChild",
                keys: ["G_ZY"],
                filter: {
                    prefix: [val],
                    specilaCode: "XY2ZY",
                    facultys: facultys,
                    grade: this.query.currentGrade,
                    degreeType: degreeType
                }
            };
            queryDic(data).then(res => {
                if (res.code === 200)
                    UTILS.fillSelect(
                        {
                            profession: "G_ZY"
                        },
                        res.data,
                        this.options
                    );
                this.options.profession = this.options.profession.map((item) => {
                    return {
                        key: item.value,
                        label: item.label + "(" + item.value + ")",
                        value: item.value
                    }
                })
            });
        },
        /**
         * params row 列表数据，type 0:申请/编辑，1：查看
         */
        jumpPage(row, type = 0) {
            let queryParams = {
                type: this.applyType
            };
            if (row && row.studentId) {
                queryParams.studentInfo = [row];
            } else {
                let stu = this.selectedStu;
                if (!stu.length) {
                    this.$message({
                        type: "warning",
                        message: "请选择需要代理申请的学生"
                    });
                    return;
                } else {
                    stu.forEach(s => {
                        if (s.status * 1 == 1) {
                            this.$message({
                                type: "warning",
                                message: "所选列表包含正在审核的学生"
                            });
                            throw "err";
                        }
                    });
                    queryParams.studentInfo = stu;
                }
            }
            //queryParams添加一个参数 reviewVisiable判断进入审核页的入口，控制审核页审核理由模块显示
            if (type == 1) {
                queryParams.type = "view";
                queryParams.reviewVisiable = false;
            } else {
                queryParams.showProxyReason = true;
            }
            if (type == 0) {
                if (
                    !row.status || //正常状态
                    row.status * 1 == 2 || //驳回
                    row.status * 1 == 3 || //已审核
                    row.status * 1 == 9 //已撤销
                ) {
                    this.$router.push({
                        path: "/moveToApply",
                        query: queryParams
                    });
                } else {
                    this.$message({
                        type: "warning",
                        message: "此学生已经处于审核状态"
                    });
                }
            } else if (type == 1) {
                this.$router.push({
                    path: "/abnormalAudit",
                    query: queryParams
                });
            }
        },
        // 获取代申请列表
        getApplyList() {
            API.insteadApplyList(this.query).then(res => {
                if (res.code === 200 && res.data)
                    this.applyList = {
                        data: res.data.list,
                        total: res.data.total_
                    };
            });
        },
        // 改变页码或者页面大小
        pageChange(val) {
            this.query.pageNum_ = val.pageNum;
            this.query.pageSize_ = val.pageSize;
            this.getApplyList();
        },
        // 选择代申请学生(表格选择)
        handleSelectionChange(val) {
            this.selectedStu = val;
        },
        // 重置
        reset() {
            let query = this.query;
            query.currentGrade = '';
            query.keyWord = '';
            query.trainingLevel = '';
            query.currentFaculty = '';
            query.currentProfession = '';
            query.trainingCategory = '';
            query.degreeType = '';
            query.formLearning = '';
            query.isStudent = '';
            this.options.profession = [];
            this.getApplyList();
        },
        // 筛选
        filterCondition() {
            this.query.pageNum_ = 1;
            this.getApplyList();
        }
    },
    computed: {
        currentUser() {
            if (this.$store.state.session.user) {
                return this.$store.state.session.user;
            }
            return JSON.parse(localStorage.getItem("user"));
        },
        mockUser() {
            if (this.$store.state.session.mockUser) {
                return this.$store.state.session.mockUser;
            }
            let sessiondata = JSON.parse(localStorage.getItem("sessiondata"));
            if (sessiondata && sessiondata.mockUser) {
                return sessiondata.mockUser;
            }
        }
    }
};
</script>
<style lang="scss" scoped>
.card-title {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;
}
</style>
